package com.msunsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.msunsoft.common.BaseService;
import com.msunsoft.exception.BusinessException;
import com.msunsoft.support.dozer.DozerMapperHelper;
import com.msunsoft.system.mybatis.dao.SysMenuMapper;
import com.msunsoft.system.mybatis.model.SysMenu;
import com.msunsoft.system.vo.MenuTreeVo;

@Service
public class MenuService extends BaseService<SysMenuMapper, SysMenu> {

    @Resource
    private UserService adminService;

    public List<MenuTreeVo> selectTreeByType(Integer officeType) {
        List<SysMenu> list = this.dao.selectAllByType(officeType);
        List<MenuTreeVo> menuVoList = new ArrayList<MenuTreeVo>();
        if ((list != null) && !list.isEmpty()) {
            for (SysMenu menu : list) {
                MenuTreeVo vo = DozerMapperHelper.map(menu, MenuTreeVo.class);
                vo.setText(vo.getName());
                menuVoList.add(vo);
            }
        }

        return genTree(menuVoList);
    }

    public int deleteById4Tree(Integer id) {
        Integer count = this.dao.selectCountChildById(id);
        if (count != 0) {
            throw new BusinessException("非法操作!该节点下存在子节点,请先删除子节点!");
        }
        return super.deleteById (id);
    }

    /**
     * 通过当前用户,查询用户有权限的菜单
     * @param id 用户id
     * @return 有权管理的菜单
     */
    public List<MenuTreeVo> selectTreeByUser(Integer id) {
        List<SysMenu> menus = null;
        // 如果是超管, 则查询所有可见菜单
        if (id == 1) {
            menus = this.dao.selectAllVisable();
        }
        // 并非超管
        else {
            List<Integer> roleIds = this.adminService.selectRoleIdListByUserId(id);
            if ((roleIds != null) && !roleIds.isEmpty()) {
                // 2. 通过角色id, 查询角色可见的菜单
                menus = this.dao.selectVisableByRoleIds(roleIds);

            }
        }
        List<MenuTreeVo> menuVoList = new ArrayList<MenuTreeVo>();
        if ((menus != null) && !menus.isEmpty()) {
            for (SysMenu menu : menus) {
                MenuTreeVo vo = DozerMapperHelper.map(menu, MenuTreeVo.class);
                vo.setText(vo.getName());
                String href = vo.getHref();
                menuVoList.add(vo);
            }
        }
        return genTree(menuVoList);
    }
    
    /**
     * 给出查询到的数据,生成树形结构, (为easyui提供树形结构)
     * @param list 查询到的数据
     * @return 树形结构
     */
    //TODO 树形结构算法
    public List<MenuTreeVo> genTree(List<MenuTreeVo> list) {

        List<MenuTreeVo> nodeList = new ArrayList<MenuTreeVo>();
        for (MenuTreeVo node1 : list) {
            boolean mark = false;
            for (MenuTreeVo node2 : list) {
                if ((node1.getParentId() != null) && node1.getParentId().equals(node2.getId())) {
                    mark = true;
                    if (node2.getChildren() == null) {
                        node2.setChildren(new ArrayList<MenuTreeVo>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if (!mark) {
                nodeList.add(node1);
            }
        }
        return nodeList;
    }
}
